import { reqGoodsDetail, reqShoppingNum } from "@/api";

const state = {
  //商品详情
  goodsInfo: {}
};

const mutations = {
  GOODSINFO(store, data) {
    store.goodsInfo = data;
  },
};

const actions = {
  //获取商品详情
  async requestGoodsInfo({ commit }, id) {
    const { data } = await reqGoodsDetail(id);
    if (data.code == 200) {
      commit("GOODSINFO", data.data);
    }
  },
  //添加购车数量
  // eslint-disable-next-line no-unused-vars
  async requestShoppingN({ commit }, { id, num }) {
    const { data } = await reqShoppingNum(id, num);
    if (data.code == 200) {
      return "ok";
    } else {
      return Promise.reject(new Error("faile"));
    }
  },
};

const getters = {
  // 这里是异步数据,当数据还没请求回来的时候,state.goodsInfo.categoryView返回的是一个undefined
  // 当数据回来的时候，才有数据
  categoryView(state) {
    return state.goodsInfo.categoryView || {};
  },
  skuInfo(state) {
    return state.goodsInfo.skuInfo || {};
  },
  //商品属性
  skuAttrValueList(state) {
    return state.goodsInfo.spuSaleAttrList || [];
  },
};

export default {
  state,
  mutations,
  actions,
  getters,
};
